package com.rfid.a252robot.admin;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.rfid.a252robot.R;
import com.rfid.common.log.LogUtils;
import com.rfid.util.ACache;
import com.rfid.util.PreferenceUtil;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TimeSetActivity extends AppCompatActivity {

    @BindView(R.id.startTime)
    TimePicker mStartTime;
    @BindView(R.id.endTime)
    TimePicker mEndTime;

    @BindView(R.id.timeInfo)
    TextView mTimeInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_set);
        ButterKnife.bind(this);
        mStartTime.setIs24HourView(true);
        mEndTime.setIs24HourView(true);

        int sh = PreferenceUtil.getInt("startHour",-1);
        int eh = PreferenceUtil.getInt("endHour",-1);
        int sm = PreferenceUtil.getInt("startMin",-1);
        int em = PreferenceUtil.getInt("endMin",-1);
        if (sh == -1) {
            mTimeInfo.setText("当前无自动巡航设置");
        } else {
            mTimeInfo.setText("自动巡航时间：" + sh + ":" + sm + "----" + eh + ":" + em);
        }

//        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//        Intent intent = new Intent(this, DailyAlarmReceiver.class);
//        intent.setAction(DailyAlarmReceiver.ACTION_START);
//        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0, intent, 0);
//        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
//                SystemClock.elapsedRealtime() + 10 * 1000,
//                pendingIntent1);

//        System.out.println("alarmManager.getNextAlarmClock().getTriggerTime() = " + alarmManager.getNextAlarmClock().getTriggerTime());

    }

    public void clear(View view) {
        PreferenceUtil.commitInt("startHour",-1);
        PreferenceUtil.commitInt("endHour",-1);
        PreferenceUtil.commitInt("startMin",-1);
        PreferenceUtil.commitInt("endMin",-1);
        mTimeInfo.setText("当前无自动巡航设置");
        cancelAlarm();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void set(View view) {
        int sh = mStartTime.getHour();
        int sm = mStartTime.getMinute();
        int eh = mEndTime.getHour();
        int em = mEndTime.getMinute();
        if (eh < sh) {
            Toast.makeText(this,"结束时间必须大于开始时间",Toast.LENGTH_SHORT).show();
            return;
        }else if (eh == sh) {
            if (em <= sm) {
                Toast.makeText(this,"结束时间必须大于开始时间",Toast.LENGTH_SHORT).show();
                return;
            }
        }
        PreferenceUtil.commitInt("startHour",sh);
        PreferenceUtil.commitInt("endHour",eh);
        PreferenceUtil.commitInt("startMin",sm);
        PreferenceUtil.commitInt("endMin",em);
        mTimeInfo.setText("自动巡航时间：" + sh + ":" + sm + "----" + eh + ":" + em);
        cancelAlarm();
//        setDailyAlarm(this,DailyAlarmReceiver.ACTION_START,sh,sm);
//        setDailyAlarm(this,DailyAlarmReceiver.ACTION_END,eh,em);
        LogUtils.v("start DailyWorkService");
        Intent intent = new Intent();
        intent.setClass(this,DailyWorkService.class);
        startService(intent);
    }

    public void cancelAlarm() {
        LogUtils.v("stop DailyWorkService");
        Intent intent = new Intent();
        intent.setClass(this,DailyWorkService.class);
        stopService(intent);
    }
}